//
//  ArchivingTest.m
//  iOS Sample Code
//
//  Created by lei yu on 11-12-11.
//  Copyright (c) 2011年 lancet. All rights reserved.
//

#import "ArchivingTest.h"

@implementation AddressCard

#define NAME @"name"
#define EMAIL @"email"


@synthesize name=name_,email=email_;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail{
    
}
-(NSComparisonResult) compareNames: (id) element{
    return [name_ compare:[(AddressCard *)element name]];
}
-(void) print{
    NSLog(@"The Address book with name:%@ ,and email:%@",name_,email_);
}
// Additional method for NSCopying protocol
-(void) retainName: (NSString *) theName andEmail: (NSString *) theEmail{
    
}

-(id)copyWithZone:(NSZone *)zone{
    AddressCard *new = [[AddressCard allocWithZone:zone]init ];
    new.name=name_;
    new.email=email_;
    return new;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:name_ forKey:NAME];
    [aCoder encodeObject:email_ forKey:EMAIL];
    
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    
    name_=[[aDecoder valueForKey:NAME]retain];
    email_=[[aDecoder valueForKey:EMAIL]retain];
    return self;   
}

-(id)copy{
    AddressCard *new = [[AddressCard alloc]init];
    new.name=name_;
    new.email=email_;
    return new;
}
@end



@implementation ArchivingTest





-(void)testPListSerialize{
    NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
    NSDictionary *glossary=
    [NSDictionary dictionaryWithObjectsAndKeys:@"A class defined so other classes can inherit from it.",@"abstract class",@"To implement all the methods defined in a protocol", @"adopt",@"Storing an object for later use. ",@"archiving",
     nil
     ];
    if ([glossary writeToFile: @"glossary" atomically: YES]== NO){ 
        NSLog (@"Save to file failed!");
    }
    [pool drain];
}


-(void)testArchiving{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
    NSString *aName=@"Julia Kochan";
    NSString *aEmail=@"jewls337@axlc.com";
    NSString *bName=@"Tony Iannino";
    
    NSString *bEmail=@"tony.iannino@techfitness.com";
    NSString *cName=@"Stephen Kochan";
    NSString *cEmail=@"steve@steve_kochan.com";
    NSString *dName=@"Jamie Baker";
    NSString *dEmail=@"jbaker@hitmail.com";
    
    AddressCard *card1=[[AddressCard alloc] init];
    AddressCard *card2=[[AddressCard alloc] init];
    AddressCard *card3=[[AddressCard alloc] init]; 
    AddressCard *card4=[[AddressCard alloc] init];
    
    
    
    [card1 setName: aName andEmail: aEmail]; 
    
    
    [card2  setName: bName andEmail: bEmail];
    [card3   setName: cName andEmail: cEmail];
    [card4  setName: dName andEmail: dEmail];
    
    NSArray *array = [NSArray arrayWithObjects:card1,card2,card3,card4, nil];
    
    if ([NSKeyedArchiver archiveRootObject: array toFile: @"addrbook.arch"]==NO){
        NSLog (@"archiving failed");
    }
    
    [array release];
    array= [NSKeyedUnarchiver unarchiveObjectWithFile: @"addrbook.arch"];
    for(AddressCard *card in array){
        [card print];
    }
    
    [card1 release];
    [card2 release];
    [card3 release]; 
    [card4 release];
    [array release];
    [pool drain];
}


-(void)testUsingNSData{
    
    NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
    
    AddressCard *myFoo1=[[AddressCard alloc] init];
    NSMutableData *dataArea;
    NSKeyedArchiver *archiver;
    NSArray *myBook;
    
    
    
    
    // Insert code from Program 19.7 to create an Address Book // in myBook containing four address cards
    [myFoo1 setName: @"This is the string"];
    [myFoo1 setEmail:@"xxx@google.com"];
    // Set up dataArea
    archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData: dataArea]; 
    [archiver encodeObject: myBook forKey: @"myaddrbook"]; 
    [archiver encodeObject: myFoo1 forKey: @"myfoo1"];
    [archiver finishEncoding];
    // Write the archived data area to a file
    if ([dataArea writeToFile: @"myArchive" atomically: YES]==NO){
        NSLog (@"Archiving failed!"); 
    }
    [archiver release];
    [myFoo1 release];
    
    
    
    
    
    
    
    
    
    dataArea=[NSData dataWithContentsOfFile: @"myArchive"];
    if (!dataArea) {
        NSLog (@"Can’t read back archive file!");
        return ;
    }
    
    NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData: dataArea];
    // Decode the objects we previously stored in the archive
    myBook =[unarchiver decodeObjectForKey: @"myaddrbook"];
    myFoo1=[unarchiver decodeObjectForKey: @"myfoo1"];
    [unarchiver finishDecoding]; [unarchiver release];
    // Verify that the restore was successful [myBook list];
    NSLog (@"%@\n%@\n", [myFoo1 name],[myFoo1 email]);
    [pool release];
    [pool drain];
    
}


-(void)testCopyingObjectsByArchiving{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; NSData *data;
    NSMutableArray *dataArray=[NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"one"], [NSMutableString stringWithString: @"two"], [NSMutableString stringWithString: @"three"], nil
                               ];
    NSMutableArray *dataArray2; NSMutableString *mStr;
    // Make a deep copy using the archiver
    data=[NSKeyedArchiver archivedDataWithRootObject: dataArray];
    dataArray2=[NSKeyedUnarchiver unarchiveObjectWithData: data];
    mStr=[dataArray2 objectAtIndex: 0];
    [mStr appendString: @"ONE"];
    NSLog (@"dataArray: ");
    for ( NSString *elem in dataArray ){
        NSLog (@"%@", elem);
    }
    NSLog (@"\ndataArray2: ");
    for ( NSString *elem in dataArray2 ){
        NSLog (@"%@", elem);
    }
    [pool drain]; 
    return;
}


@end
